// pages/partExersdetail/partExerdetail.js
var httpUtils = require('../../url.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    exercisesList:[],
    problemId : 0,
    bc_default: '#FBFBFB',
    bc_right: '#98FB98',
    bc_wrong: '#FF99B4',
    bcA: '',
    bcB: '',
    bcC: '',
    bcD: '',
    index: -1,
    choose: 0,
    judge:-1,
    chooseId:'',
    subject:''
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //分章练习的章节号以及学科号
    var sub = options.sub;
    console.log('章节号:'+sub);
    //获取收藏数组
    let collect = wx.getStorageSync("collect") || [];
    var baseURL = httpUtils.url;
    var app = getApp();
    wx.request({
      url: baseURL+'exercise/tiku2/'+sub,
      method : "GET",
      header : {
        'content-type' : 'application/json'
      },
      success : (res) => {
        // var exercisesList =[111,11];
        // console.log(res.data)
        this.setData({
          exercisesList : res.data,
          subject: sub
        })
        console.log(this.data.exercisesList);
        //用于给pickerMsg数组赋值
        var len = this.data.exercisesList.length;
        var tmppickerMsg = new Array();
        for(var i = 0; i <len; i++){
          tmppickerMsg[i] = (i+1)+'.'+this.data.exercisesList[i].problem.substring(0,22);
        }
        //用于第一道题目是否收藏的判断
        let index = this.findIndex(collect,this.data.exercisesList[this.data.problemId].problemId);
        let flag = index==-1?0:1;
        let index2 = 0;
        //获取当前章节的做题记录
        if(sub=='java1'){
          index2 = app.globalData.java1Id;
        }else if(sub=='java2'){
          index2 = app.globalData.java2Id;
        }else if(sub=='java3'){
          index2 = app.globalData.java3Id;
        }else if(sub=='java4'){
          index2 = app.globalData.java4Id;
        }else if(sub=='java5'){
          index2 = app.globalData.java5Id;
        }else if(sub=='java6'){
          index2 = app.globalData.java6Id;
        }else if(sub=='java7'){
          index2 = app.globalData.java7Id;
        }else if(sub=='java8'){
          index2 = app.globalData.java8Id;
        }else if(sub=='java9'){
          index2 = app.globalData.java9Id;
        }else if(sub=='java10'){
          index2 = app.globalData.java10Id;
        }else if(sub=='java11'){
          index2 = app.globalData.java11Id;
        }
        this.setData({
          pickerMsg: tmppickerMsg,
          collectFlag:flag,
          problemId:index2
        })
      }
    })

  },
 
  btnOpClick(event){
    var tmpchoose = event.target.id;
    var app = getApp();
    if(this.data.choose == 1) return;
     this.setData({
      choose:1
    })
    console.log(event.target.id);
    var choose = event.target.id;
    if(this.data.exercisesList[this.data.problemId].answer==choose){
      if (choose == 'A') {
        this.setData({ bcA: this.data.bc_right });
      }
      else if (choose == 'B') {
        this.setData({ bcB: this.data.bc_right });
      }
      else if (choose == 'C') {
        this.setData({ bcC: this.data.bc_right });
      }
      else if (choose == 'D') {
        this.setData({ bcD: this.data.bc_right });
      }
      this.setData({
        judge: 1,
        chooseId: tmpchoose,
        choose:1
      })
    }else {
      if (choose == 'A') {
        this.setData({ bcA: this.data.bc_wrong });
      }
      else if (choose == 'B') {
        this.setData({ bcB: this.data.bc_wrong });
      }
      else if (choose == 'C') {
        this.setData({ bcC: this.data.bc_wrong });
      }
      else if (choose == 'D') {
        this.setData({ bcD: this.data.bc_wrong });
      }
      //选择错误，加入错题本
      this.setData({
        judge: 0,
        chooseId: tmpchoose,
        choose:1
      })
      var baseURL = httpUtils.url;
       //发起错题请求
       wx.request({
        url: baseURL+'user/error',
        data:{
          problemId: this.data.exercisesList[this.data.problemId].problemId,
          openId: this.data.id
        },
        method : "GET",
        header : {
          'content-type' : 'application/json'
        },
        success : (res) => {
          // var exercisesList =[111,11];
          console.log("添加错题:"+res.data.result);
        }
      })
    }
  },
  showAnsewr(){
    this.setData({
      trueShow:1
    })
  },
  nextExercise(){
    var that = this;
    var app = getApp();
    if(that.data.problemId < this.data.exercisesList.length-1){
       // 1.获取缓存中的题目收藏数组
       let collect = wx.getStorageSync("collect") || [];
       // 2.判断该题目是否被收藏过
       let index = this.findIndex(collect,this.data.exercisesList[that.data.problemId + 1].problemId);
       let i = index==-1 ? 0 : 1;
      this.setData({
          problemId: that.data.problemId + 1,
          judge:0,
          // 否则答案会一直显示出来
          choose : 0,
          // 切换题目时判断下一题是否收藏
          collectFlag: i,
          //将按钮颜色初始化
          bcA: this.data.bc_default,
          bcB: this.data.bc_default,
          bcC: this.data.bc_default,
          bcD: this.data.bc_default,
          trueShow:0
      })
       //存储做题记录
       var sub = this.data.subject;
       if(sub=='java1'){
        app.globalData.java1Id = this.data.problemId;
      }else if(sub=='java2'){
        app.globalData.java2Id = this.data.problemId;
      }else if(sub=='java3'){
        app.globalData.java3Id = this.data.problemId;
      }else if(sub=='java4'){
        app.globalData.java4Id = this.data.problemId;
      }else if(sub=='java5'){
        app.globalData.java5Id = this.data.problemId;
      }else if(sub=='java6'){
        app.globalData.java6Id = this.data.problemId;
      }else if(sub=='java7'){
        app.globalData.java7Id = this.data.problemId;
      }else if(sub=='java8'){
        app.globalData.java8Id = this.data.problemId;
      }else if(sub=='java9'){
        app.globalData.java9Id = this.data.problemId;
      }else if(sub=='java10'){
        app.globalData.java10Id = this.data.problemId;
      }else if(sub=='java11'){
        app.globalData.java11Id = this.data.problemId;
      }
    }
  },
  lastExercise(){
    var that = this;
    var app = getApp();
    if(that.data.problemId > 0){
      // 1.获取缓存中的题目收藏数组
      let collect = wx.getStorageSync("collect") || [];
      // 2.判断该题目是否被收藏过
      let index = this.findIndex(collect,this.data.exercisesList[that.data.problemId - 1].problemId);
      let i = index==-1 ? 0 : 1;
        this.setData({
          problemId: that.data.problemId - 1,
          judge:0,
          choose : 0,
          // 否则答案会一直显示出来
          // 切换题目时判断上一题是否收藏
          collectFlag: i,
           //将按钮颜色初始化
           bcA: this.data.bc_default,
           bcB: this.data.bc_default,
           bcC: this.data.bc_default,
           bcD: this.data.bc_default,
           trueShow:0
      })
      var sub = this.data.subject;
      console.log(sub);
       if(sub=='java1'){
        app.globalData.java1Id = this.data.problemId;
      }else if(sub=='java2'){
        app.globalData.java2Id = this.data.problemId;
      }else if(sub=='java3'){
        app.globalData.java3Id = this.data.problemId;
      }else if(sub=='java4'){
        app.globalData.java4Id = this.data.problemId;
      }else if(sub=='java5'){
        app.globalData.java5Id = this.data.problemId;
      }else if(sub=='java6'){
        app.globalData.java6Id = this.data.problemId;
      }else if(sub=='java7'){
        app.globalData.java7Id = this.data.problemId;
      }else if(sub=='java8'){
        app.globalData.java8Id = this.data.problemId;
      }else if(sub=='java9'){
        app.globalData.java9Id = this.data.problemId;
      }else if(sub=='java10'){
        app.globalData.java10Id = this.data.problemId;
      }else if(sub=='java11'){
        app.globalData.java11Id = this.data.problemId;
      }
    }
  },
  handleCollect(event){
    console.log(event);
    // 默认：没有被收藏
    let isCollect = false;
    // 1.获取缓存中的题目收藏数组
    let collect = wx.getStorageSync("collect") || [];
    console.log(collect);
    // 2.判断该题目是否被收藏过
    //event.currentTarget.id是数据库中题目的唯一标识Id
    let index = this.findIndex(collect,event.currentTarget.id);
    // 3.当index!= -1 表示  已经收藏过了
    if (index !== -1) {
      // 能找到  已经收藏过了  在数组中删除该题目
      collect.splice(index, 1);
      // 取消收藏
      isCollect = false;
      var baseURL = httpUtils.url;
      wx.request({
        url: baseURL+'user/unstar',
        data:{
          uid: this.data.id,
          problemId: this.data.exercisesList[this.data.problemId].problemId
        },
        method : "GET",
        header : {
          'content-type' : 'application/json'
        },
        success : (res) => {
          console.log('题目取消收藏：'+res.data.result);
        }
      })
    }else {
      // 没有收藏过
      collect.push(this.data.exercisesList[this.data.problemId]);
      // 添加收藏
      isCollect = true;
      var  myDate =  new Date();
      var myTime = myDate.toLocaleDateString();
      console.log(this.data.id)
      var baseURL = httpUtils.url;
      wx.request({
        url: baseURL+'user/star',
        data:{
          //不知为啥，用uid就会失败！！后端获取不到，可能冲突？
          openId: this.data.id,
          problemId: this.data.exercisesList[this.data.problemId].problemId,
          startime: myTime
        },
        method : "GET",
        header : {
          'content-type' : 'application/json'
        },
        success : (res) => {
          console.log('题目收藏：'+res.data.result);
        }
      })
    }
    // 4.把数组存入缓存中
    wx.setStorageSync("collect", collect);
    // 5.修改data中的数据
    this.setData({
      collectFlag: isCollect
    })
  },
  findIndex(array,id){
    var element;
    for (let index = 0; index < array.length; index++) {
       element = array[index];
      if(element.problemId == id){
        return index;
      }
    }
    return -1;//表示没找到
  },
  bindPickerChange(event){
    console.log(event);
    console.log(event.detail.value);
    this.setData({
      //注意: 一定要转换成int，否则会出现问题（problemId会变掉的）
      problemId: parseInt(event.detail.value)
    })
    this.setQuestion();
    //注意：变完之后还得重置收藏状态
     // 1.获取缓存中的题目收藏数组
     let collect = wx.getStorageSync("collect") || [];
     console.log(collect);
     // 2.判断该题目是否被收藏过
     //event.currentTarget.id是数据库中题目的唯一标识Id
     let index = this.findIndex(collect,this.data.exercisesList[this.data.problemId].problemId);
     this.setData({
       collectFlag: index==-1?0:1
     })
  },
  setQuestion(){

  },
  showAnsewr(){
    this.setData({
      trueShow:1
    })
  }
})